/**
 * ExtJS的一些封装
 */

function ajaxRequest(settings) {
	// 参数对象
	var params = settings.params === undefined ? {} : settings.params,
		showWaiting = settings.showWaiting === undefined ? true : settings.showWaiting,
		showMsg = settings.showMsg === undefined ? false : settings.showMsg,
		timeout = settings.timeout === undefined ? 60 * 1000 : settings.timeout,
		// isUpload = settings.isUpload === undefined ? false : settings.isUpload,
		// form = settings.form === undefined ? "" : settings.form;
	// 发送请求
		waiting = null;
	if (showWaiting) {
		waiting = Ext.Msg.wait('正在处理，请稍等...', '', '');
	}
	Ext.Ajax.request({
		url: settings.url,
		params: params,
		timeout: timeout,
        // isUpload : isUpload,
        // form : form,
		success: function (response, options) {
			if (waiting != null) {
				waiting.hide();
			}
			var json = Ext.decode(response.responseText);
			if (json.success == true) {
				if (showMsg == true) { // 显示提示信息
					// 请求成功时的提示文字
					var successMsg = "操作成功";
					if (json.msg && json.msg != '') {
						successMsg = json.msg;
					}
					showSucMsg(successMsg);
				}
				if (settings.callback) { // 回调方法
					settings.callback(json);
				}
			} else if (json.success == false) {
				var message = '发生异常.';
				if (json.msg && json.msg != '') { // 后台设定的业务消息
					message = json.msg;
				} else if (settings.failureMsg && settings.failureMsg != '') { // 前台指定的错误信息
					message = settings.failureMsg;
				}
				showFailMsg(message);
			}
		},
		failure: function (response, options) {
			if (waiting != null) {
				waiting.hide();
			}
		}
	});
};

function showSucMsg(msg){
	var notification = new Ext.ux.Notification();
	msg = (msg==undefined)?"":msg;
	notification.showMessage("提示", "操作成功!<br>" + msg, true);
}

function showFailMsg(msg){
	var notification = new Ext.ux.Notification();
	notification.showMessage("提示", "操作失败!<br>"+msg, false);
}

//function genStatusCombo(){
//	var statusStore = new Ext.data.ArrayStore({
//		data : [[0, "停用"], [1, "启用"]],
//		fields : ["status", "display"]
//	});
//	var statusCombo = new Ext.form.ComboBox({
//		fieldLabel : "状态",
//		store : statusStore,
//		editable : false,
//		width : 70,
//		mode : "local",
//		triggerAction : "all",
//		valueField : "status",
//		displayField : "display",
//		hiddenName : "status",
//		resizable : true,
//		allowBlank : false
//	});
//	return statusCombo;
//}

function genEnumStatusCombo(){
	var statusStore = new Ext.data.ArrayStore({
		data : [["DISABLED", "停用"], ["ENABLED", "启用"]],
		fields : ["status", "display"]
	});
	var statusCombo = new Ext.form.ComboBox({
		fieldLabel : "状态",
		store : statusStore,
		editable : false,
		width : 70,
		mode : "local",
		triggerAction : "all",
		valueField : "status",
		displayField : "display",
		hiddenName : "status",
		resizable : true,
		allowBlank : false
	});
	return statusCombo;
}

function genQueryStatusCombo(){
	var statusStore = new Ext.data.ArrayStore({
		data : [[-1, "全部"], [0, "停用"], [1, "启用"]],
		fields : ["status", "display"]
	});
	var statusCombo = new Ext.form.ComboBox({
		fieldLabel : "状态",
		store : statusStore,
		width : 70,
		editable : false,
		mode : "local",
		triggerAction : "all",
		valueField : "status",
		displayField : "display",
		hiddenName : "status",
		resizable : true,
		allowBlank : false
	});
	return statusCombo;
}

function genStatusCombo(){
	var statusStore = new Ext.data.ArrayStore({
		data : [["DISABLED", "停用"], ["ENABLED", "启用"]],
		fields : ["status", "display"]
	});
	var statusCombo = new Ext.form.ComboBox({
		fieldLabel : "状态",
		store : statusStore,
		width : 70,
		editable : false,
		mode : "local",
		triggerAction : "all",
		valueField : "status",
		displayField : "display",
		hiddenName : "status",
		resizable : true,
		allowBlank : false
	});
	return statusCombo;
}

function genGenderCombo(){
	var genderStore = new Ext.data.ArrayStore({
		data : [["MALE", "男"], ["FEMALE", "女"]],
		fields : ["gender", "display"]
	});
	var genderCombo = new Ext.form.ComboBox({
		fieldLabel : "性别",
		store : genderStore,
		width : 70,
		editable : false,
		mode : "local",
		triggerAction : "all",
		valueField : "gender",
		displayField : "display",
		hiddenName : "gender",
		resizable : true,
		allowBlank : false
	});
	return genderCombo;
}

function genQueryField(){
	return new Ext.form.TextField({
		width : 70
	});
}

function genMajorCombo(){
	var majorStore = new Ext.data.JsonStore({
		url : "admin/major/list",
		fields : ["id", "zymc", "zydm"],
		root : "data"
	});
	return new Ext.form.ComboBox({
		fieldLabel : "专业",
		store : majorStore,
		width : 70,
		editable : false,
		resizable : true,
		mode : "remote",
        valueField: "zydm",
		displayField:"zymc",
		hiddenName : "zydm",
		triggerAction : "all"
	});
}

function genClazzCombo(){
	var clazzStore = new Ext.data.JsonStore({
		url : "admin/clazz/listInSchoolClazz",
		totalProperty : "total",
		root : "rows",
//		autoLoad : true,
		fields : ["id", "name", "status", "enterSchoolDate", "graduateDate",
			"createDate", "updateDate", "des", "authorName", "owner"
		]
	});
	var clazzCombo = new Ext.form.ComboBox({
		store : clazzStore,
		fieldLabel : "班级",
		editable : false,
		resizable : true,
		width : 70,
		mode : "remote",
		displayField:"name",
		valueField: "id",
		triggerAction : "all",
		hiddenName : "clazzId"
	});
	return clazzCombo;
}

function genOptBarByFn(create, del, update, toggle, importIn, exportOut, download, view){
	var createBtn = {
		text : "创建",
		iconCls : "create",
		handler : create
	};
	var delBtn = {
		text : "删除",
		iconCls : "delete",
		handler : del
	};
	var updateBtn = {
		text : "更新",
		iconCls : "update",
		handler : update
	};
	var toggleBtn = {
		text : "切换状态",
		iconCls : "toggleStatus",
		handler : toggle
	};
	var importInBtn = {
		text : "导入",
		iconCls : "import",
		handler : importIn
	};
	var exportOutBtn = {
		text : "导出",
		iconCls : "export",
		handler : exportOut
	}
	var downloadBtn = {
		text : "下载",
		iconCls : "download",
		handler : download
	}
	var viewBtn = {
		text : "浏览",
		iconCls : "view",
		handler : view
	}
	var optBar = [];
	if(create!=null){
		optBar.push(createBtn);
		optBar.push("-");
	}
	if(del!=null){
		optBar.push(delBtn);
		optBar.push("-");
	}
	if(update!=null){
		optBar.push(updateBtn);
		optBar.push("-");
	}
	if(toggle!=null){
		optBar.push(toggleBtn);
		optBar.push("-");
	}
	if(importIn!=null){
		optBar.push(importInBtn);
		optBar.push("-");
	}
	if(exportOut!=null){
		optBar.push(exportOutBtn);
		optBar.push("-");
	}
	if(download!=null){
		optBar.push(downloadBtn);
		optBar.push("-");
	}
	if(view!=null){
		optBar.push(viewBtn);
		optBar.push("-");
	}
	return optBar;
}

/**
 *
 * @param create
 * @param del
 * @param update
 * @param toggle
 * @returns {Array}
 */
function genCRUDOptBar(create, del, update, toggle){
	return [{
		text : "创建",
		iconCls : "create",
		handler : create
	}, "-", {
		text : "删除",
		iconCls : "delete",
		handler : del
	}, "-", {
		text : "更新",
		iconCls : "update",
		handler : update
	}, "-", {
		text : "切换状态",
		iconCls : "toggleStatus",
		handler : toggle
	}]
}
/**
 *
 * @param create
 * @param del
 * @param update
 * @param toggle
 * @param exportIn
 */
function genCRUDUOptBar(create, del, update, toggle, importIn){
	return [{
		text : "创建",
		iconCls : "create",
		handler : create
	}, "-", {
		text : "删除",
		iconCls : "delete",
		handler : del
	}, "-", {
		text : "更新",
		iconCls : "update",
		handler : update
	}, "-", {
		text : "切换状态",
		iconCls : "toggleStatus",
		handler : toggle
	}, {
		text : "导入",
		iconCls : "import",
		handler : importIn
	}]
}

function genYesOrNotRadioGroup(label, name){
	return new Ext.form.RadioGroup({
		fieldLabel : label,
		items : [{
			boxLabel : '否',
			inputValue : '0',
			name : name
		}, {
			boxLabel : '是',
			name : name,
			inputValue : '1',
			checked : true
		}]
	});
}

function submitForm(form, callback){
	var waiting = Ext.Msg.wait('正在处理，请稍等...', '', '');
	form.getForm().submit({
		success : function(f, action){
			if (waiting != null) {
				waiting.hide();
			}
			var msg = action.result.msg;
			var win = form.findParentByType("window");
			if(win!=undefined){
				win.close();
			}
			callback(action);
			showSucMsg(msg);
		},
		failure : function(f, action){
			if (waiting != null) {
				waiting.hide();
			}
			var msg = action.result.msg;
			showFailMsg(msg);
		}
	});
}

function confirmDelete(callback){
	Ext.MessageBox.confirm("系统提示", "你确认要删除吗？", function(e) {
		if (e == "yes") {
			callback();
		}
	});
}

/**
 * 生成grid、editGrid的分页工具栏的配置项
 * @param {} store 必须参数 指定进行分页的store
 * @param {} limit 可选参数 指定分页的大小，默认值为20
 * @return {} 配置项json
 */
function genPageBar(store, limit){
	var bbar = {
		xtype:'paging',
		store : store,
		pageSize : limit==undefined?15:limit,
		displayInfo : true,
		plugins : new Ext.ux.Andrie.pPageSize(),
		displayMsg : '显示第 {0}-{1} 条记录，一共 {2} 条',
		emptyMsg : '没有记录'
	}
	return bbar;
}



function renderDate(v){
	if(v==undefined || v==""){
		return "";
	}
	return new Date(v).format("Y-m-d")
//	return Ext.util.Format.dateRenderer('Y-m-d H:i:s');
}

function renderTime2Minutes(v){
	if(v==undefined || v==""){
		return "";
	}
	return new Date(v).format("Y-m-d H:i")
}

function renderTime2Second(v){
    if(v==undefined || v==""){
        return "";
    }
    return new Date(v).format("Y-m-d H:i")
}

function renderTimeNoYear(v){
    if(v==undefined || v==""){
        return "";
    }
    return new Date(v).format("m-d H:i:s")
}

function renderStatus(data, cell){
	switch (data) {
		case 'DISABLED' :
			return "<span><font color='red'>停用</font></span>";
			break;
		case 'ENABLED' :
			return "<span><font color='green'>启用</font></span>";
			break;
		default :
			return "<span><font color='purple'>错误</font></span>";
			break;
	}
}

function renderEnumStatus(data, cell){
	switch (data) {
		case "DISABLED" :
			return "<span><font color='red'>停用</font></span>";
			break;
		case "ENABLED" :
			return "<span><font color='green'>启用</font></span>";
			break;
		default :
			return "<span><font color='purple'>错误</font></span>";
			break;
	}
}

function renderEnumGender(data, cell){
	switch (data) {
		case 'FEMALE' :
			return "<span><font color='green'>女</font></span>";
			break;
		case 'MALE' :
			return "<span><font color='blue'>男</font></span>";
			break;
		default :
			return "<span><font color='purple'>错误</font></span>";
			break;
	}
}

function renderYesOrNo(data, cell){
	switch (data){
		case 0 :
			return "<span><font color='gray'>否</font></span>";
			break;
		case 1 :
			return "<span><font color='green'>是</font></span>";
			break;
		default :
			return "<span><font color='purple'>错误</font></span>";
			break;
	}
}

function renderLightBlue(data, cell){
	cell.attr = "style=background-color:lightblue";
	return data;
}



/**
 * 表单重置
 */
function resetForm(formId) {
	Ext.getCmp(formId).form.reset();
}

/**
 * 表单校验
 */
function formValid(formId) {
	var formValidBol = Ext.getCmp(formId).form.isValid();
	return formValidBol;
}

/**
 * 操作成功提示
 */
//function showSucMsg(config) {
//	Ext.Msg.show({
//				title : config.title||'提示',
//				msg : config.msg||'',
//				width:300,
//				buttons : Ext.Msg.OK,
//				icon : Ext.MessageBox.INFO,
//				fn : config.fn||''
//			});
//}

/**
 * 操作失败提示
 */
//function showFailMsg(config) {
//	Ext.Msg.show({
//				title : config.title||'提示',
//				value : config.msg||'',
//				minWidth :config.width||300,
//				msg:config.msg||'操作失败!原因:',
//				multiline : true,
//				defaultTextHeight :30,
//				prompt:true,
//				buttons : Ext.Msg.OK,
//				icon : Ext.MessageBox.ERROR,
//				fn : config.fn||''
//			});
//}

/**
 * 询问提示
 */
function askMesg(config) {
	Ext.Msg.show({
				title : config.title||'提示',
				msg : config.msg||'',
				width:300,
				buttons : Ext.Msg.OKCANCEL,
				icon : Ext.MessageBox.QUESTION,
				fn : config.fn||''
			});
}

/**
 * 警告提示
 */
function warningMesg(config) {
	Ext.Msg.show({
				title : config.title||'提示',
				msg : config.msg||'',
				width:300,
				buttons : Ext.Msg.OK,
				icon : Ext.Msg.WARNING,
				fn : config.fn||''
			});
}

/**
 * ajax提交返回结果
 * @param {} response
 * @return {Boolean}
 */
function ajaxRequestResult(returnData){
	var failMesg = returnData.failMesg;
	if(failMesg == undefined)return true;
	if(failMesg == 'error'){
		showFailMesg({
			msg:'请联系管理员!'
		});
		return false;
	}else{
		failMesg = failMesg.replace('com.bhtec.exception.ApplicationException:','');
		showFailMesg({
			msg:failMesg
		});
		return false;
	} 
	return true;
}

/**
 * 修改删除页面选择
 */
function modifyDelSelRecord(cmpId) {
	var record = '';
	var count = Ext.getCmp(cmpId).getSelectionModel().getSelections().length;
	if (count == 0 || count > 1) {
		showSucMesg({
						msg:'请选择一条记录!'
					});
		return record;
	}
	return Ext.getCmp(cmpId).getSelectionModel().getSelected().data;
}
/**
 * 操作时判断是否选择了记录
 * @param {} cmpId
 */
function isSelRecord(cmpId) {
	var record = '';
	var count = Ext.getCmp(cmpId).getSelectionModel().getSelections().length;
	if (count == 0 ) {
		showSucMesg({
						msg:'请选择一条或多条记录!'
					});
		return false;
	}
	return true;
}

/**
 * 刷新列表
 */
function refreshGridList(gridId) {
	if(gridId != ''){
		var grid = Ext.getCmp(gridId);
		var store = grid.store;
		store.reload();
	}
}

/**
 * 查询填充列表
 */
function queryFillGridList(gridId,returnData) {
	if(gridId != ''){
		var gridList = getExtCmpById(gridId);
		var gridStore = gridList.store;
		gridStore.loadData(returnData, false);
	}
}
/**
 * grid获得选择的记录ids
 * @param {} idName
 * @param {} gridId
 * @return {}
 */
function getDelRedIds(idName,gridId){
		var records = Ext.getCmp(gridId).getSelectionModel().getSelections();
		var delRecIds = '';
		for(i=0;i<records.length;i++){
			var recdId = eval('records['+i+'].data.'+idName);
			delRecIds = delRecIds==''?recdId:delRecIds+','+recdId; 
		}
		return delRecIds;
}

/**
 * grid获得选择的记录ids
 * @param {} idName
 * @param {} gridId
 * @return {}list
 */
function getColumnRecordIds(idName,gridId){
		var records = Ext.getCmp(gridId).getSelectionModel().getSelections();
		var colRecIds = new Array();
		for(i=0;i<records.length;i++){
			var recdId = eval('records['+i+'].data.'+idName);
			colRecIds.push(recdId);
		}
		return colRecIds;
}

/**
 * grid获得多选择ids
 * @param {} idName
 * @param {} gridId
 * @return {}
 */
function getMultiSelectedRedIds(idName,gridId){
		var records = Ext.getCmp(gridId).getSelectionModel().getSelections();
		var multiSelectedRedIds = '';
		for(i=0;i<records.length;i++){
			var recdId = eval('records['+i+'].data.'+idName);
			multiSelectedRedIds = multiSelectedRedIds==''?recdId:multiSelectedRedIds+','+recdId; 
		}
		return multiSelectedRedIds;
}

/**
 * 获得ext组件
 */
function getExtCmpById(id){
	return Ext.getCmp(id);
}

/**
 * 获得ext组件值
 */
function getExtCmpValueById(id){
	return Ext.getCmp(id).getValue();
}

/**
 * 将ext组件置空
 */
function resetCmpValueById(id){
	return Ext.getCmp(id).setValue('');
}

/**
 *  根据ext组件id,设置值
 */
function setCmpValueById(id,value){
	return Ext.getCmp(id).setValue(value);
}

/**
 * 格式化日期格式Y-M-D
 * @param {} value
 * @return {}
 */
function formatDateToYMD(value){
		if(value != null){
		     return value.substr(0,10);
		}
}
/**
 * 格式化日期格式Y-M-D H:I:S
 * @param {} value
 * @return {}
 */
function formatDateToYMDHIS(value){
		if(value != null){
			return Ext.util.Format.date(value,'Y-m-d h:i:s');
		}
}

/**
 * 为表单增加回车事件
 * @param {}
 */
function formEnterEvent(event) {
	var all = Ext.DomQuery.select('input[type!=hidden] '); // 查找所有非隐藏的录入向（ext中select都是用input模拟的所以这里不用找select）
	// 遍历并添加enter的监听
	Ext.each(all, function(o, i, all) {
				Ext.get(o).addKeyMap({
					key : 13,
					fn : function() {
						try {
							if((i+1) < all.length){
								var n = Ext.getCmp(all[i+1].id);
								Ext.getCmp(all[i+1].id).focus();
							}
						} catch (e) {
							Ext.getCmp(all[i+1].id).focus();
						}
						return true;
					}
				})
			});
	document.body.focus(); // 使页面获取焦点，否则下面设定默认焦点的功能有时不灵验
}

/**
 * 判断某id对应的组件是否的定义
 * @param {} id 组件id
 * @return {Boolean} 组件若定义返回组件，未定义返回false
 */
function isDefined(id){
	var component = Ext.getCmp(id);
	if(undefined == component){
		return false;
	}
	return component;
}

/**
 * 判断某字符串是否为空引用或内容为空
 * @param {} str 字符串
 * @return {Boolean} 
 */
function isNullOrEmpty(str){
	if(null == str || "" == str){
		return true;
	}else{
		return false;
	}
}

//视图工具，包括生成Ext控件
Ext.ns("Util.view");
/**
 * 动态加载window的js文件，显示window
 * @param {} moduleName
 */
Util.view.showWindow = function(moduleName, gridId){
	if(!Util.control.isRegistered(moduleName)){
		loadJsFileByEval(moduleName ,add);
	}else{
		add();
	}
	function add(){
		var addWindow = new Util.control.getModule(moduleName)(gridId);
		addWindow.show();
	}
}

Util.view.loadMask = function(config){
	var loadMask = new Ext.LoadMask(Ext.getBody(), {
		msg : config.msg||"数据处理中,请稍候..."
	});
	return loadMask;
}

/**
 * 生成grid、editGrid的分页工具栏的配置项
 * @param {} store 必须参数 指定进行分页的store
 * @param {} limit 可选参数 指定分页的大小，默认值为20
 * @return {} 配置项json
 */
Util.view.genPagingBarCfg = function(store, limit){
	var bbar = {
		xtype:'paging',
		store : store,
		pageSize : limit==undefined?20:limit,
		displayInfo : true,
		plugins : new Ext.ux.Andrie.pPageSize(),
		displayMsg : '显示第 {0}-{1} 条记录，一共 {2} 条',
		emptyMsg : '没有记录'
	}
	return bbar;
}

Util.view.genNativePagingBarCfg = function(store, limit){
	var bbar = {
		xtype:'paging',
		store : store,
		pageSize : limit==undefined?20:limit,
		displayInfo : true,
		displayMsg : '共{2}条记录',
		emptyMsg : '没有记录'
	}
	return bbar;
}

/**
 * 生成window的按钮数组
 * @param {} formId		formId
 * @param {} windowId	windowId
 * @param {} saveFucn	保存按钮句柄
 * @return {}			按钮数组
 */
Util.view.genWindowButtons = function(formId, windowId, saveFucn){
	var buttons = [{
		text : "保存",
		iconCls : "button_save",
		handler : saveFucn
	}, {
		text : "重置",
		iconCls : "button_reset",
		handler : function(){
			var form = Ext.getCmp(formId);
			form.getForm().reset();
		}
	}, {
		text : "取消",
		iconCls : "button_cancle",
		handler : function(){
			var window = Ext.getCmp(windowId);
			window.close();
		}
	}]
	return buttons;
}

/**
 * 
 * @param {} callback	用户点击"yes"的提示后执行的回调
 */
Util.view.confirmDel = function(callback){
	Ext.MessageBox.confirm("系统提示", "你确认要删除吗？", function(e) {
		if (e == "yes") {
			callback();
		}
	});
}



Util.view.rendererEditableStatus = function(data, cell){
	cell.attr = "style=background-color:lightblue";
	switch (data) {
		case 0 :
			return "<span><font color='green'>启用</font></span>";
			break;
		case 1 :
			return "<span><font color='red'>停用</font></span>";
			break;
		default : 
			return "<span><font color='purple'>未知状态</font></span>";
			break;
	}
}

//控制工具
Ext.ns("Util.control");
/**
 * 对ext的ajax的封装，增加了loadmask
 * @param {} config
 */
Util.control.ajaxRequest = function(config) {
	//提交等待提示
	var mask;
	if(config.noMask == undefined){
		mask = Util.view.loadMask(config);
		mask.show();
	}
	Ext.Ajax.request({
		url : config.url||'',
		params : config.params||'',
		success : function(response, options) {
			var returnData = Ext.util.JSON.decode(response.responseText);
			if(returnData){
				var suc = returnData.success;
				if(!suc){
					Util.view.showFailureOperate("请联系管理员!");
				}
			}
			var callMethod = function(method){        
		        method(returnData);
		    }
		    if(config.callBack != undefined){
				callMethod(config.callBack);//提交成功回调
		    }
			if(config.noMask == undefined){
				mask.hide();
			}
		},
		failure : function(response, options) {
			showFailMesg({
				msg:'请联系管理员.失败类型：'+response.statusText
			});
			if(config.noMask == undefined)
				mask.hide();
		}
	});
}

/**
 * form 	FormPanel实体对象
 * params	参数列表
 * callback	操作成功里的非异步回调
 */
Util.control.submitForm = function(config){
	var basicForm = config.form.getForm();
	if(!basicForm.isValid()){
		return false;
	}
	var mask = Util.view.loadMask({});
	mask.show();
	basicForm.submit({
//		clientValidation : true,
		params : config.params||'',
		success : function(form, action){
			var returnData = action.result;
			var callMethod = function(method){
		        method(returnData);
		    }
		    if(config.callback != undefined){
		    	callMethod(config.callback);//提交成功回调
		    }
			Util.view.showSuccessOperate();
			mask.hide();
		},
		failure : function(form, action) {
			var msg = action.result.msg;
			Util.view.showFailureOperate(msg);
			mask.hide();
		}
	});
}

Util.control.registerModule = function registerModule(moduleName, moduleReference){
	spec.view.ModuleMap[moduleName] = moduleReference;
}

Util.control.isRegistered = function(moduelName){
	if(Util.control.getModule(moduelName) == undefined){
		return false;
	}else{
		return true;
	}
}

Util.control.getModule = function(moduleName){
	return spec.view.ModuleMap[moduleName];
}

/**
 * 处理按钮的可用、禁用
 * @param {} buttons	待处理button构成的数组
 * @param {} enabled	可用还是禁用
 */
Util.control.dealButtons = function(buttons, enabled){
	for(var i=0; i<buttons.length; i++){
		if(enabled==true){
			buttons[i].enable();
		}else{
			buttons[i].disable();
		}
	}
}

/**
 * 适用条件	textfield->tbar->grid
 * 使用方法	为textfield的specialkey事件配置此函数
 * @param {} field
 * @param {} e
 */
Util.control.enterKeyHandler = function(field, e){
    if (e.getKey() == Ext.EventObject.ENTER){
//        var tbar = field.ownerCt;
//        var grid = tbar.ownerCt;
//        var store = grid.getStore();
//        store.load();
    	field.ownerCt.ownerCt.getStore().load();
    }
}

Util.control.reloadTabpanelItem = function(moduleName){
	var item = Ext.getCmp(moduleName);
	var tabsPanel = Ext.getCmp("tabpanel");
	tabsPanel.remove(item);
	item = new Util.control.getModule(moduleName)();
	tabsPanel.add(item);
	tabsPanel.setActiveTab(item);
}

/**
 * 生成操作面板的tbar
 * @param fullTbar  预定义的完整tbar
 * @param moduleId  数据库里module的主键，等同操作面板的id
 * @returns {*}
 */
function genToolBar(optBar, queryBar, moduleId){
    var coms = genModuleCommand(moduleId);
    var tbar = [];
    var _tbar =  $.grep(optBar, function(t){
        if(t == "-" || t == "->"){
            return false;
        }
        var flag = false;
        for(var i=0; i<coms.length; i++){
            if(t.text == coms[i].chinese){
                flag = true;
                break;
            }
        }
        return flag;
    });
    $.map(_tbar, function(v){
        tbar.push(v);
        tbar.push("-")
    });
    $.map(coms, function(v){
        if(v.chinese == "查询"){
            tbar.push("->");
            tbar.push(queryBar);
        }
    });
    return tbar;

    function genModuleCommand(moduleId){
        var coms = $.grep(userCommand, function(v){
            return (v.module.id == moduleId);
        });
        return coms;
    }
}